﻿using System;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Xml;
using BitsOfJoy.Core.web;

namespace BitsOfJoy.temp
{
    public partial class bit : BOJPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);
            LoadColors();
        }

        private void LoadColors()
        {
            var xmlFile = Server.MapPath("bitvalues.xml");
            var doc = new XmlDocument();
            try
            {
                doc.Load(xmlFile);
            }
            catch
            {
                return;
            }

            if(doc.DocumentElement == null)
            {
                return;
            }

            var bitNodes = doc.DocumentElement.SelectNodes("child::bit");

            if (bitNodes == null)
            {
                return;
            }

            foreach( XmlNode bitNode in bitNodes)
            {
                var element = (ImageButton)FindControl(bitNode.Attributes["ID"].Value);
                element.Style["background-color"] = ColorFromXmlNode(bitNode.SelectSingleNode("child::color")).Name;
            }
        }

        private static Color ColorFromXmlNode(XmlNode node)
        {
            var s = node.InnerText;
            return Color.FromName(s);
        }

        protected void toggleColor(object sender, EventArgs e)
        {
            var element = (ImageButton) sender;
            element.BackColor = GetNextColor(Color.FromName(element.Style["background-color"]));
            StoreNewColor(element);
        }

        private void StoreNewColor(WebControl element)
        {
            var xmlFile = Server.MapPath("bitvalues.xml");
            var doc = new XmlDocument();
            try
            {
                doc.Load(xmlFile);
            }
            catch
            {
                return;
            }

            if(doc.DocumentElement == null)
            {
                doc.AppendChild(doc.CreateNode(XmlNodeType.Element, "bits", ""));
            }
            var n = doc.DocumentElement.SelectSingleNode(string.Format("child::bit[attribute::ID=\"{0}\"]", element.ID));
            if(n == null)
            {
                CreateBitElement(doc, element);
            }
            else
            {
                n.SelectSingleNode("child::color").InnerText = element.BackColor.Name;
            }
            doc.Save(xmlFile);
        }

        private static void CreateBitElement(XmlDocument doc, WebControl element)
        {
            var attr = doc.CreateAttribute("ID");
            attr.Value = element.ID;
            var bit = doc.CreateNode(XmlNodeType.Element, "bit", "");
            bit.Attributes.Append(attr);

            var color = doc.CreateNode(XmlNodeType.Element, "color", "");
            color.InnerText = element.BackColor.Name;
            bit.AppendChild(color);

            doc.DocumentElement.AppendChild(bit);
        }

        private static Color GetNextColor( Color currentColor)
        {
            if (currentColor.Equals(Color.Red))
                return Color.Blue;
            if (currentColor.Equals(Color.Blue))
                return Color.Green;
            if (currentColor.Equals(Color.Green))
                return Color.Yellow;
            if (currentColor.Equals(Color.Yellow))
                return Color.Purple;
            if (currentColor.Equals(Color.Purple))
                return Color.Lime;
            if (currentColor.Equals(Color.Lime))
                return Color.Red;
            return Color.Red;
        }

        protected void refreshTimer_Tick(object sender, EventArgs e)
        {
            
        }
    }
}
